/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */

package team.mai.antexpress.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.mai.antexpress.dao.DepartmentDAO;
import team.mai.antexpress.dao.UserDAO;
import team.mai.antexpress.entities.DepartmentEntity;
import team.mai.antexpress.entities.PermissionGroupEntity;

import java.util.List;

@Service
public class DepartmentService {

	public static final int SUCCESS = 0;
	public static final int ERROR = -1;
	public static final int INSUFFICIENT_PRIVILEGE = -2;
	public static final int USER_IN_DEPARTMENT = -3;

	@Autowired
	private DepartmentDAO departmentDAO;
	@Autowired
	private UserDAO userDAO;

	public int insertDepartment(String operatorName, DepartmentEntity dept) {
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyDepartment()) {
			return departmentDAO.insertDepartment(dept);
		} else {
			return INSUFFICIENT_PRIVILEGE;
		}
	}

	public int deleteDepartment(String operatorName, int deptId) {
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyDepartment()) {
			if(departmentDAO.hasUserInDepartment(deptId)){
				return USER_IN_DEPARTMENT;
			}
			if (departmentDAO.deleteDepartment(deptId)) {
				return SUCCESS;
			} else {
				return ERROR;
			}

		} else {
			return INSUFFICIENT_PRIVILEGE;
		}
	}

	public int deleteUsersInDepartment(String operatorName, int deptId){
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyDepartment()) {
			if (departmentDAO.deleteUsersInDepartment(deptId)) {
				return SUCCESS;
			} else {
				return ERROR;
			}

		} else {
			return INSUFFICIENT_PRIVILEGE;
		}
	}

	@Deprecated
	public int deleteDepartments(String operatorName, List<Integer> ids) {
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyDepartment()) {
			if (departmentDAO.deleteDepartments(ids)) {
				return SUCCESS;
			} else {
				return ERROR;
			}
		} else {
			return INSUFFICIENT_PRIVILEGE;
		}
	}

	public int updateDepartment(String operatorName, DepartmentEntity dept) {
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyDepartment()) {
			if (departmentDAO.updateDepartment(dept)) {
				return SUCCESS;
			} else {
				return ERROR;
			}
		} else {
			return INSUFFICIENT_PRIVILEGE;
		}

	}

	public List<DepartmentEntity> getAllDepartments(String operatorName) {
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
		if (permissionByUser.isAdmin() || permissionByUser.isLookupDepartment()) {
			return departmentDAO.getAllDepartments();
		} else {
			return null;
		}

	}

	public DepartmentEntity getDepartment(String operatorName, Integer id) {
		if (id == null)
			return null;
		else {
			PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
			if (permissionByUser.isAdmin() || permissionByUser.isLookupDepartment()) {
				return departmentDAO.getDepartment(id);
			} else {
				return null;
			}
		}
	}

	@Deprecated
	public DepartmentEntity getDepartment(String operatorName, String deptName) {
		if (deptName == null) {
			return null;
		} else {
			PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(operatorName);
			if (permissionByUser.isAdmin() || permissionByUser.isLookupDepartment()) {
				return departmentDAO.getDepartment(deptName);
			} else {
				return null;
			}
		}
	}
}
